#!/usr/bin/env python

import sys
from time import strptime

def mapper():
    framewidth=6
    bucket=[]
    term=''
    li=position=0
    mtag={}
    ctag=0
    stag=0
    atype={}
    #(query_term,session_id,date,s_time,e_time,modif_tag,click_tag,satif_tag,act_type,position,distance)=(None)*11
    for line in sys.stdin:
        line = line.decode('gbk','ignore').strip().encode('utf-8')
        li+=1
        if len(line)>1:
            if len(line.split('\t'))!=7:
                continue
            (session_id,date_time,modif_tag,query_term,url,satif_tag,act_type) = line.split('\t')
            date_time=strptime(date_time, '%d/%b/%Y:%H:%M:%S')
            date=str(date_time.tm_year)+'/'+str(date_time.tm_mon)+'/'+str(date_time.tm_mday)
            ctime=date_time.tm_hour*60*60+date_time.tm_min*60+date_time.tm_sec
            if query_term==term:
                mt=mtag.get(modif_tag,0)
                mtag[modif_tag]=mt+1
                if len(url)>1:
                    ctag|=1
                stag|=int(satif_tag)
                atpe=atype.get(act_type,0)
                atype[act_type]=atpe+1
            else:
                e_time=ctime
                if term!='':
                    distance=li-position
                    bucket.append((term,s_time,e_time,mtag,ctag,stag,atype,position,distance))
                term=query_term
                s_time=ctime
                mtag={}
                mtag[modif_tag]=1
                ctag=0
                if len(url)>1:
                    ctag=1
                stag=int(satif_tag)
                atype={}
                atype[act_type]=1
                     
        else:
            if len(bucket)<2:
                continue
            
            for i in range(len(bucket)-1):
                ceil=i+framewidth+1
                if ceil > len(bucket):
                    ceil=len(bucket)
                for j in range(i+1,ceil):
                    btime=bucket[j][1]-bucket[i][2]
                    bm_tag=[bucket[i][3],bucket[j][3]]
                    bc_tag=[bucket[i][4],bucket[j][4]]
                    bs_tag=[bucket[i][5],bucket[j][5]]
                    ba_type=[bucket[i][6],bucket[j][6]]
                    bpos=bucket[i][7]
                    bdis=bucket[j][8]
                    pvalue=(session_id,date,btime,bm_tag,bc_tag,bs_tag,ba_type,bpos,bdis)
                    print '%s||| %s\t1\t%s' % (bucket[i][0],bucket[j][0],pvalue)
            position=li
            bucket=[] 

if __name__ == '__main__':
    mapper()
